/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import presentation.controller.ControlPanelMiCuenta;
import presentation.controller.ControlPanelRegistro;
import vo.AlumnoVO;

/**
 *
 * @author Joanna 8a
 */
public class PanelRegistro extends javax.swing.JPanel {

    /**
     * Creates new form PanelRegistro
     */
    public PanelRegistro(FramePrincipal parent, String accion) {
        this.parent = parent;
        initComponents();
        jTextField_Username.requestFocusInWindow();
        if(accion.equals("Modificar")){
            jButton_Registrar.setText(accion);
            jTextField_Username.setEditable(false);
            llenarCampos();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_Volver = new javax.swing.JButton();
        jButton_Registrar = new javax.swing.JButton();
        jPasswordField2 = new javax.swing.JPasswordField();
        jComboBox_Carrera = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField_Nombre = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField_Apellidos = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Username = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jDateChooser_FechaNacimiento = new com.toedter.calendar.JDateChooser();

        jButton_Volver.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButton_Volver.setText("Volver");
        jButton_Volver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_VolverActionPerformed(evt);
            }
        });

        jButton_Registrar.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButton_Registrar.setText("Registrar");
        jButton_Registrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_RegistrarActionPerformed(evt);
            }
        });

        jPasswordField2.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jPasswordField2.setText("jPasswordField2");
        jPasswordField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordField2FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField2FocusLost(evt);
            }
        });

        jComboBox_Carrera.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jComboBox_Carrera.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ingeniería de Sistemas", "Ingeniería Electrónica", "Ingeniería Civil", "Ingeniería Eléctrica", "Ingerniería Ambiental", "Ingeniería Química", "Derecho", "Ciencias Políticas", "Trabajo Social", "Biología", "Farmacia", "Enfermería", "Física", "Química", "Matemáticas" }));

        jLabel3.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel3.setText("Confirma contraseña");

        jLabel4.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel4.setText("Nombre");

        jTextField_Nombre.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel5.setText("Apellidos");

        jTextField_Apellidos.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel6.setText("Fecha de nacimiento");

        jLabel7.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel7.setText("Carrera");

        jPasswordField1.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jPasswordField1.setText("jPasswordField1");
        jPasswordField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordField1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField1FocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel2.setText("Contraseña");

        jTextField_Username.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel1.setText("Usuario");

        jDateChooser_FechaNacimiento.setDate(restriccionFecha(16));
        jDateChooser_FechaNacimiento.setDateFormatString("dd/MM/yyyy");
        jDateChooser_FechaNacimiento.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jDateChooser_FechaNacimiento.setMaxSelectableDate(restriccionFecha(14));
        jDateChooser_FechaNacimiento.setMinSelectableDate(restriccionFecha(100));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_Registrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton_Volver))
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jTextField_Username)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                    .addComponent(jPasswordField2)
                    .addComponent(jTextField_Nombre)
                    .addComponent(jTextField_Apellidos)
                    .addComponent(jComboBox_Carrera, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jDateChooser_FechaNacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(jTextField_Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField_Nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField_Apellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooser_FechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox_Carrera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Registrar)
                    .addComponent(jButton_Volver))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_VolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_VolverActionPerformed
        if(jButton_Registrar.getText().equals("Registrar")){
            parent.cambiarPanel(new PanelLogin(parent));
        }
        else{
            parent.cambiarPanel(new PanelPrincipal(parent));
        }
    }//GEN-LAST:event_jButton_VolverActionPerformed

    private void jButton_RegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_RegistrarActionPerformed
        AlumnoVO vo = new AlumnoVO();
        javax.swing.JButton boton = (javax.swing.JButton) evt.getSource();
        
        vo.setUsername(jTextField_Username.getText());
        vo.setNombre(jTextField_Nombre.getText());
        vo.setApellido(jTextField_Apellidos.getText());
        vo.setPassword(new String(jPasswordField1.getPassword()));
        vo.setCarrera(jComboBox_Carrera.getSelectedItem().toString());
        vo.setFechaDeNacimiento(jDateChooser_FechaNacimiento.getDate());
        vo.setUsuarioActivo(new ControlPanelRegistro().ColocarIpUsuario() + ".true");

        ControlPanelRegistro cpr = new ControlPanelRegistro();
        String mg = "";
        if (boton.getText().equals("Registrar")) {
            mg = cpr.registrar(vo, new String(jPasswordField2.getPassword()));
        } 
        else if(boton.getText().equals("Modificar")){
            mg = cpr.ModificarAlumno(vo, new String(jPasswordField2.getPassword()));
        }
        JOptionPane.showMessageDialog(this, mg, "Aviso", JOptionPane.INFORMATION_MESSAGE, null);
        if (mg.equals(cpr.ERRORPSWRD)) {
            jPasswordField1.requestFocus();
            jPasswordField1.selectAll();
        } else if (mg.equals(cpr.OK)) {
            parent.cambiarPanel(new PanelMaterias(parent));
            parent.getjMenuBar1().setVisible(true);
            parent.setResizable(true);
        } else if (mg.equals(cpr.USEREXIST)) {
            jTextField_Username.requestFocus();
            jTextField_Username.selectAll();
        }
        else if(mg.equals(cpr.OK_MODIFICACION)){
            parent.cambiarPanel(new PanelPrincipal(parent));
            parent.getjMenuBar1().setVisible(true);
        }
        
    }//GEN-LAST:event_jButton_RegistrarActionPerformed

    private void jPasswordField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField1FocusGained
        String text = String.valueOf(jPasswordField1.getPassword());
        if (text.equals("jPasswordField1")) {
            jPasswordField1.setText(null);
        }
    }//GEN-LAST:event_jPasswordField1FocusGained

    private void jPasswordField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField1FocusLost
        if (jPasswordField1.getPassword().length == 0) {
            jPasswordField1.setText("jPasswordField1");
        }
    }//GEN-LAST:event_jPasswordField1FocusLost

    private void jPasswordField2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField2FocusGained
        String text = String.valueOf(jPasswordField2.getPassword());
        if (text.equals("jPasswordField2")) {
            jPasswordField2.setText(null);
        }
    }//GEN-LAST:event_jPasswordField2FocusGained

    private void jPasswordField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField2FocusLost
         if (jPasswordField2.getPassword().length == 0) {
            jPasswordField2.setText("jPasswordField2");
        }
    }//GEN-LAST:event_jPasswordField2FocusLost
    
    public Date restriccionFecha (int restriccion){
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.YEAR, g.get(Calendar.YEAR) - restriccion);
        return g.getTime();
    }
    
    private void llenarCampos(){
        AlumnoVO al = new ControlPanelMiCuenta().FindByIp();
        
        jTextField_Username.setText(al.getUsername());
        jPasswordField1.setText(al.getPassword());
        jPasswordField2.setText(al.getPassword());
        jTextField_Nombre.setText(al.getNombre());
        jTextField_Apellidos.setText(al.getApellido());
        jDateChooser_FechaNacimiento.getDateEditor().setDate(al.getFechaDeNacimiento());
        jComboBox_Carrera.setSelectedItem(al.getCarrera());
    }
    
    private FramePrincipal parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Registrar;
    private javax.swing.JButton jButton_Volver;
    private javax.swing.JComboBox jComboBox_Carrera;
    private com.toedter.calendar.JDateChooser jDateChooser_FechaNacimiento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextField_Apellidos;
    private javax.swing.JTextField jTextField_Nombre;
    private javax.swing.JTextField jTextField_Username;
    // End of variables declaration//GEN-END:variables
}
